﻿$(function () {
    'use strict';
    layui.use(['form', 'element', 'notice', 'table', 'iziToast'], function () {
        var form = layui.form,
            element = layui.element,
            notice = Util.notice !== null && Util.notice() !== null
                ? Util.notice() : layui.notice,
            iziToast = layui.iziToast,
            table = layui.table;
        var lodash = _,
            tableIns = null,
            initAdminUserTable = function (tableconfig) {
                return table.render(tableconfig);
            };

        if (adminusertableconfig) {
            tableIns = initAdminUserTable(adminusertableconfig);
        }
        //监听表格数据行按钮
        let editurl = "/system/adminusermanage/edit",
            editWidowConfig = {
                title: "编辑/修改",
                type: 2,
                content: "",
                fixtolayoutbody: true,
                anim: 3,
                //width: 720,
                //height: 850
                end: function () {
                    tableIns.reload(adminusertableconfig);
                }
            },
            currentSystemModuleId = $("#currentSystemModuleId").val();
        table.on('tool(adminusermanagetable)', function (obj) {
            var data = obj.data,
                thisElem = this,
                buttonid = layui.$(thisElem).data("buttonid");
            if (obj.event === 'Edit') {
                editWidowConfig.content = editurl + "?moduleid=" + currentSystemModuleId + "&buttonid=" + buttonid + "&userid=" + data.Id;
                Util.OpenMainWindow(editWidowConfig);
            } else if (obj.event === 'Delete') {
                layer.confirm("确定要进行删除操作吗？", function (confirmIndex) {
                    layer.close(confirmIndex);
                    iziToast.loading("删除中，请稍候...");
                    let node = new Object(DeleteNodeModel);
                    node.EntityId = data.Id;
                    node.ButtonId = buttonid;
                    node.ModuleId = currentSystemModuleId;
                    //$.deleteJSON("/api/system/adminusermanage/accountdelete", node, function (res) {
                    //    iziToast.destroy();
                    //    if (!httpapi.checkResponse(res)) {
                    //        //输入信息校验错误
                    //        notice.error(res.message);
                    //    } else {//Success
                    //        notice.success(res.message);
                    //        obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    //    }
                    //});
                    httpapi.fetchPost("/api/system/adminusermanage/accountdelete", node).then(res => {
                        iziToast.destroy();
                        if (!httpapi.checkResponse(res)) {
                            //输入信息校验错误
                            notice.error(res.message);
                        } else {//Success
                            notice.success(res.message);
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                        }
                    }).catch(error => consol.error(error));
                });
            }
        });
        /* 表格搜索 */
        form.on('submit(tbAdvSearch)', function (data) {
            lodash.assignIn(adminusertableconfig.where, data.field);
            if (tableIns !== null) {
                let options = {
                    where: adminusertableconfig.where
                    , page: {
                        curr: 1 //重新从第 1 页开始
                    }
                };
                tableIns.reload(options);
            }
            return false;
        });
        //监听顶部按钮栏
        var PageButtonEvent = {
            Add: function (buttonid) {
                editWidowConfig.content = editurl + "?moduleid=" + currentSystemModuleId + "&buttonid=" + buttonid;
                Util.OpenMainWindow(editWidowConfig);
                return false;
            }
        };
        $('.pagebuttongroup .layui-btn').on('click', function (e) {
            e.preventDefault();
            var type = $(this).data('type');
            var buttonid = $(this).data("buttonid");
            PageButtonEvent[type] ? PageButtonEvent[type].call(this, buttonid) : '';
        });
    });
});